package org.whatever.aha.zjut.platform.util;

import lombok.RequiredArgsConstructor;
import org.whatever.aha.zjut.base.constant.ErrorCode;
import org.whatever.aha.zjut.base.constant.StatusConstant;
import org.whatever.aha.zjut.base.exception.AppException;
import org.whatever.aha.zjut.platform.dto.project.ProjectDto;
import org.whatever.aha.zjut.platform.dto.project.ProjectMemberDto;
import org.whatever.aha.zjut.platform.entity.competition.CompetitionStage;
import org.whatever.aha.zjut.platform.entity.competition.CompetitionStageStatus;
import org.whatever.aha.zjut.platform.service.competition.CompetitionService;
import org.whatever.aha.zjut.platform.service.project.ProjectMemberService;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Date;
import java.util.List;

/**
 * @author Vc
 * @version 1.0
 * @Desc 校验接口工具类
 * @date 2021/10/13 21:07
 */
public class CheckUtil {
    /**
     * 校验接口调用是否符合生命周期
     *
     * @param start
     * @param end
     */
    public static void checkTimeLimit(Date start, Date end){
        Date date = new Date();
        if(date.before(start) || date.after(end)){
            throw new AppException(ErrorCode.TIME_CHECK_FAILED);
        }
    }

    /**
     * 确认项目是否合法
     *
     * @param members
     * @param teamNum
     */
    public static void checkProjectMember(List<ProjectMemberDto> members, List<Integer> memberIds, List<Integer> memberRanks, int teamNum, int userId){
        if(members != null && members.size() > 0){
            if(members.size() > teamNum){
                throw new AppException(ErrorCode.TEAM_NUM_LIMIT);
            }

            members.forEach(memberDto -> {
                if(userId == memberDto.getMemberId()){throw new AppException(ErrorCode.TEAM_LEADER_ALREADY_EXIST);}
                if(memberIds.contains(memberDto.getMemberId())){ throw new AppException(ErrorCode.TEAM_MEMBER_ALREADY_EXIST);}
                if(memberDto.getProjectMemberRank() > teamNum+1){ throw new AppException(ErrorCode.ILLEGAL_MEMBER_MAX_RANK);}
                if(memberRanks.contains(memberDto.getProjectMemberRank())){ throw new AppException(ErrorCode.RANK_ALREADY_EXIST);}
                memberRanks.add(memberDto.getProjectMemberRank());
                memberIds.add(memberDto.getMemberId());
            });
        }
    }



    /**
     * 确认项目提交操作是否合法 ( 提交阶段 && 项目未提交 ) ||  (被退回 && 审核阶段)
     *
     * @param singleSubmitCompStageStatus 提交阶段
     * @param singleJudgeCompStageStatus  审核阶段
     * @param curStudentStatus  当前项目状态
     * @return
     */
    public static void checkStageStatus(CompetitionStageStatus singleSubmitCompStageStatus, CompetitionStageStatus singleJudgeCompStageStatus, int curStudentStatus){
        Date date = new Date();
        if( !((date.before(singleSubmitCompStageStatus.getStatusEndTime()) && date.after(singleSubmitCompStageStatus.getStatusStartTime()) && curStudentStatus == StatusConstant.PROJECT_STUDENT_STATUS_WAIT_FOR_SUBMIT.getCode()) ||
                (date.before(singleJudgeCompStageStatus.getStatusEndTime()) && date.after(singleSubmitCompStageStatus.getStatusStartTime()) && curStudentStatus == StatusConstant.PROJECT_STUDENT_STATUS_RETURN.getCode())) ){
            throw new AppException(ErrorCode.OPERATION_FORBIDDEN_STAGE_STATUS);
        }
    }

    /**
     * 确认当前竞赛阶段状态是否和预期状态一致
     *
     * @param status
     * @param curStatus
     */
    public static void checkStuStatus(int status, int curStatus){
        if(status != curStatus){
            throw new AppException(ErrorCode.OPERATION_FORBIDDEN_STAGE_STATUS);
        }
    }

    /**
     * 确认当前时间对应的阶段信息
     *
     * @param stage
     * @param competitionStageStatus
     * @param date
     * @param stageFinalOrder
     * @return
     */
    public static boolean checkStageStatusByTime(CompetitionStage stage, CompetitionStageStatus competitionStageStatus, Date date, int stageFinalOrder){
        // 修复初赛 复赛 交接的空白期，getCompStatusInfo接口显示的应该是复赛未开始
        // if ((stage.getStageOrder() == StatusConstant.COMPETITION_STAGE_PRELIMINARY.getCode() && date.before(competitionStageStatus.getStatusStartTime()) && competitionStageStatus.getStatusOrder() == StatusConstant.COMPETITION_STAGE_STATUS_ORDER_SUBMIT.getCode()) ||
        if ((date.before(competitionStageStatus.getStatusStartTime()) && competitionStageStatus.getStatusOrder() == StatusConstant.COMPETITION_STAGE_STATUS_ORDER_SUBMIT.getCode()) ||
                (date.after(competitionStageStatus.getStatusStartTime()) && date.before(competitionStageStatus.getStatusEndTime())) ||
                (stage.getStageOrder() == stageFinalOrder && date.after(competitionStageStatus.getStatusEndTime()) && competitionStageStatus.getStatusOrder() == StatusConstant.COMPETITION_STAGE_STATUS_ORDER_PROMULGATE.getCode())) {
            //提交开始前为提交状态,在开始和结束时间内为该状态,当前时间晚于公示结束时间（且无后续阶段），为公示阶段
            return true;
        }
        return false;
    }


//    /**
//     * 确认院级管理员修改项目操作权限
//     * @param userAcademyId
//     * @param projectAcademyId
//     */
//    public static void checkAcademyAdminModifyProjectPermission(int userAcademyId, int projectAcademyId, int projectSchoolRecStatus, Date startTime, Date endTime){
//        //确认用户拥有项目修改权限(同一个学院)
//        if(userAcademyId != projectAcademyId){
//            throw new AppException(ErrorCode.NOT_PERMITTED);
//        }
//        //学院信息提交状态为未提交 即学校状态为 1：不可见
//        if(projectSchoolRecStatus != StatusConstant.PROJECT_STATUS_SCHOOL_VERSION_INVISIBLE.getCode()){
//            throw new AppException(ErrorCode.PROJECT_HAS_BEEN_SUBMIT);
//        }
//        //提交和审核阶段
//        checkTimeLimit(startTime, endTime);
//    }


}
